﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reactive.Linq;
using System.Threading;
using System.Windows.Threading;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Threading;
using TfL;
using Tfl;
namespace Tfl.Controls.WPF.ViewModel
{
    public class DesignTimeLineStatusViewModel : LineStatusViewModel
    {
        public DesignTimeLineStatusViewModel()
        {
            Title = "Design Time";
            base.LineStatuses = new ObservableCollection<SingleLineStatusViewModel>();
            foreach(string name in TfL.LineCodes.Names)
            {
                LineStatuses.Add(new SingleLineStatusViewModel {Line=name, Color = TfL.LineCodes.ToColor(name).ToString(),Description="Description",Status="Ok" });
            }
        }
    }

    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class LineStatusViewModel : ViewModelBase
    {
        public LineStatusViewModel()
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the LineStatusViewModel class.
        /// </summary>
        public LineStatusViewModel(ILineStatusFeed feed)
        {
            DispatcherHelper.Initialize();
            Title = "Run time";
            _tokenSource = new CancellationTokenSource();
            _feed = feed;

            var lineStatus = from s in _feed.Poll(_tokenSource.Token, 30000)
                             select s;

            _subcriber = lineStatus.Subscribe(OnLineStatus);

            _lineStatuses = new ObservableCollection<SingleLineStatusViewModel>();
            _lineStatuses.CollectionChanged += (s, e) => RaisePropertyChanged("LineStatuses");
        }


        public ObservableCollection<SingleLineStatusViewModel> LineStatuses
        {
            get { return _lineStatuses; }
            set
            {
                _lineStatuses = value;
                RaisePropertyChanged("LineStatuses");
            }
        }

        public string Title
        {
            get { return _title; }
            set 
            { 
                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        private void OnLineStatus(List<LineStatus> list)
        {
            if (list == null)
            {
                DispatcherHelper.CheckBeginInvokeOnUI(() => _lineStatuses.Clear());
                Title = string.Format("Line Status - Latest Update {0}", DateTime.Now);
            }
            else
                list.ForEach((status) =>
                               {
                                   SingleLineStatusViewModel svm = new SingleLineStatusViewModel
                                                                       {
                                                                           Line = status.Line.Name,
                                                                           Color =
                                                                               TfL.LineCodes.ToColor(status.Line.Name).
                                                                               ToString(),
                                                                           Description = status.StatusDetails,
                                                                           Status = status.Status.Description
                                                                       };

                                   DispatcherHelper.CheckBeginInvokeOnUI(() => _lineStatuses.Add(svm));
                               });


        }

        
        
        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
        /// 
        /// 
        private readonly ILineStatusFeed _feed;

        private readonly CancellationTokenSource _tokenSource;

        private IDisposable _subcriber;

        private ObservableCollection<SingleLineStatusViewModel> _lineStatuses;

        private List<SingleLineStatusViewModel> _lines = new List<SingleLineStatusViewModel>();

        private string _title = string.Empty;

        private bool _clearList;
    } 


}